/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sxu.shadowdrawabledemo.slice;

import com.sxu.shadowdrawable.ShadowDrawable;
import com.sxu.shadowdrawabledemo.ResourceTable;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import java.util.ArrayList;
import java.util.List;

/*******************************************************************************
 * Description:
 *
 * Author: Freeman
 *
 * Date: 2019/2/12
 *
 * Copyright: all rights reserved by Freeman.
 *******************************************************************************/
public class PagerExampleSlice extends Fraction {
    private PageSlider pageSlider;
    private List<String> dataList = new ArrayList<>();
    private int PADDING;
    private static final int PADDING_SIZE = 12;
    private static final int PROPORTION = 3;
    private static final int DATALIST_SIZE = 5;
    private static final int SHAPERADIUS = 8;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        pageSlider = new PageSlider(getContext());
        PADDING = PADDING_SIZE * PROPORTION;
        pageSlider.setPadding(PADDING, PADDING, PADDING, PADDING);

        for (int i1 = 0; i1 < DATALIST_SIZE; i1++) {
            dataList.add("Item_ " + i1);
        }
        pageSlider.setProvider(new PageSliderProvider() {
            @Override
            public int getCount() {
                return dataList.size();
            }

            @Override
            public Object createPageInContainer(ComponentContainer componentContainer, int i) {
                DependentLayout layout = (DependentLayout)scatter.parse(
                        ResourceTable.Layout_item_tab, null, false);
                Text text = (Text)layout.findComponentById(ResourceTable.Id_des);
                text.setText(dataList.get(i));
                ShadowDrawable.setShadowDrawable(text, Color.getIntColor("#2979FF"), SHAPERADIUS * PROPORTION,
                        Color.getIntColor("#fe2979FF"), PADDING, 0, 0);
                componentContainer.addComponent(layout);
                return layout;
            }

            @Override
            public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
                componentContainer.removeComponent((Component)o);
            }

            @Override
            public boolean isPageMatchToObject(Component component, Object o) {
                return false;
            }
        });
        return pageSlider;
    }

    @Override
    protected void onActive() {
        super.onActive();
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
    }
}
